package com.lovo.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.lovo.system.dto.UserDto;
import com.lovo.system.mapper.UserMapper;
import com.lovo.system.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.xml.transform.Result;

@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, UserDto> implements IUserService {

    @Autowired
    UserMapper userMapper;

    @Override
    public IPage<UserDto> cutByItem(int curPage, int pageSize, String name) {
        QueryWrapper qw = new QueryWrapper();
        if (name !=null && !"".equals(name)){
            qw.like("user_name",name);
        }
        return userMapper.selectPage(new Page<>(curPage,pageSize),qw);
    }

    @Override
    public UserDto selectOne(int id) {
        return userMapper.queryById(id);
    }

    @Override
    public int insertUser(UserDto userDto) {
        userMapper.insert(userDto);
        return userDto.getId();
    }

    @Override
    public String deleteUser(int id) {
        try {
            userMapper.deleteById(id);
            return "删除成功";
        }catch (Exception e){
            return "删除失败";
        }
    }

    @Override
    public String updateUserPassWord(int id,String password) {
        try {
            userMapper.updateUserPassWord(id,password);
            return "修改成功";
        }catch (Exception e){
            return "修改失败";
        }
    }

    @Override
    public String selectPassword(int id) {
        return userMapper.selectPassword(id);
    }

    @Override
    public int selectLogin(UserDto userDto) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("user_name",userDto.getUserName());
        qw.eq("password",userDto.getPassword());
        return userMapper.selectCount(qw);
    }

    @Override
    public UserDto selectUser(UserDto userDto) {
        QueryWrapper qw = new QueryWrapper();
        qw.eq("user_name",userDto.getUserName());
        return userMapper.selectOne(qw);
    }

}
